﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Background.Core.DAL;
using Commons.Web;
using Commons.Entity;
using Background.Core.Entity;
namespace Background.Core.Controllers
{
    public class WebManageUsersController : BaseController
    {
        WebManageUsersRepository webManageUsersRepository;
        public WebManageUsersController()
        {
            webManageUsersRepository = new WebManageUsersRepository();
        }
        //
        // GET: /WebManageUsers/

        public ActionResult Index(int? page = 1)
        {
            var model = webManageUsersRepository.GetWebManageUsers(null, new PagingParam(page.Value, base.PageSize));
            ViewBag.list = model;
            return View(model);
        }

        //
        // GET: /WebManageUsers/Details/5

        public ActionResult Details(int id)
        {
            return View(webManageUsersRepository.GetEntity<WebManageUsers>(id));
        }

        //
        // GET: /WebManageUsers/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /WebManageUsers/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {

            // TODO: Add insert logic here
            WebManageUsers entity = new WebManageUsers()
            {
                CreateDate = DateTime.Now,
                UpdateDate = DateTime.Now,
                Status = 100,
            };

            TryUpdateModel(entity);
            ModelState.Remove("CreateDate");
            ModelState.Remove("UpdateDate");
            if (ModelState.IsValid)
            {
                try
                {
                    webManageUsersRepository.Insert(entity);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }

            return View();
        }

        //
        // GET: /WebManageUsers/Edit/5

        public ActionResult Edit(int id)
        {
            return View(webManageUsersRepository.GetEntity<WebManageUsers>(id));
        }

        //
        // POST: /WebManageUsers/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, WebManageUsers entity)
        {
            try
            {
                // TODO: Add update logic here
                webManageUsersRepository.Update<WebManageUsers>(i => new WebManageUsers
                {
                    ManageUserID = entity.ManageUserID,
                    RealName = entity.RealName,
                    Mobile = entity.Mobile,
                    Email = entity.Email,
                    UpdateDate = DateTime.Now,
                    Status = entity.Status,
                });
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /WebManageUsers/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                webManageUsersRepository.Delete(new WebManageUsers { ManageUserID = id });
                return RedirectArtDialogToAction("操作成功", "Index");
            }
            catch (Exception)
            {

                return RedirectArtDialogToAction("操作失败", "Index");
            }

        }

    }
}
